package com.wu.neihan.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import com.api.util.HttpUtil;
import com.wu.neihan.model.Gather;
import com.wu.neihan.service.CommentQueueService;
import com.wu.neihan.service.PostQueueService;
import com.wu.neihan.service.QueueProduce;
import com.wu.neihan.service.QueueService;
import com.wu.neihan.service.UserQueueService;
import com.wu.util.ConMysql;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class MainContral
 */
@WebServlet(name = "JinJingZheng", urlPatterns = "/login", loadOnStartup = 1)
public class JinJingZheng extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	public void init() throws ServletException {
		// ConMysql.getCon();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String postUr="";
		String phone = request.getParameter("phone");
		String code = request.getParameter("code");
		String type = request.getParameter("type");
		if (type.equals("duanxin")) {
			// 发送短信
			postUr="https://api.accidentx.zhongchebaolian.com/mobile/mobile/global/verification";
			String body = HttpUtil.Post(postUr,"%7B%22phone%22%3A%22"+phone+"%22%7D","utf-8", false,"");
			System.out.println("发送短信"+body);
			response.setCharacterEncoding("gb2312");
			response.getWriter().append(body);
		}else if(type.equals("denglu")){
			 //第二步登陆 	
			postUr="https://api.accidentx.zhongchebaolian.com/mobile/mobile/global/login";
			JSONObject pam=new JSONObject();
			pam.put("phone", phone);
			pam.put("valicode", code);
			pam.put("devicetype", "1");
			pam.put("vertype", "1");
			pam.put("deviceid", UUID.randomUUID().toString());
			pam.put("source", "0");
			pam.put("citycode", "1101");
			System.out.println(pam.toString());
			System.out.println(HttpUtil.getURLEncoderString(pam.toString()));
			String body = HttpUtil.Post(postUr,HttpUtil.getURLEncoderString(pam.toString()),"utf-8", false,"");
			System.out.println("登陆"+body);
			JSONObject user=new JSONObject();
			user=JSONObject.fromObject(body);
			//获取车辆信息
			JSONArray resCar=getCarInfos(user.getString("userid"),phone);
			response.setCharacterEncoding("gb2312");
			response.getWriter().append(resCar.toString());
		}else if(type.equals("check")){
			ConMysql con=new ConMysql();
			con.getCon();
			String userid=con.getUserid("select jjzx_userid from jjz_main where phone='"+phone+"' limit 1");
			con.closeCon();
			//获取车辆信息
			JSONArray resCar=getCarInfos(userid,phone);
			response.setCharacterEncoding("utf-8");
			response.getWriter().append(resCar.toString());
		}
	}

	private JSONArray getCarInfos(String userid,String phone) {
		JSONArray resCar=new JSONArray();
		String postUr="https://api.jinjingzheng.zhongchebaolian.com/enterbj/platform/enterbj/entercarlist";
		String pamS="";
		pamS="userid="+userid;
		pamS=pamS+"&"+"appsource="+"jjzx";
		//System.out.println(pamS);
		try {
		String body=HttpUtil.Post(postUr,pamS,"UTF-8", false,"");
		System.out.println("获取车辆信息"+body);
		JSONObject carInfo=new JSONObject();
		carInfo=JSONObject.fromObject(body);
		
		if(carInfo.containsKey("rescode")&&carInfo.getString("rescode").equals("200")){
		//获取到数据	
			JSONArray datalist=carInfo.getJSONArray("datalist");//几辆车
			ConMysql con=new ConMysql();
			con.getCon();
			for(int i=0;i<datalist.size();i++){
				JSONArray carapplyarr=datalist.getJSONObject(i).getJSONArray("carapplyarr");//几个进京证
				for(int j=0;j<carapplyarr.size();j++){
					JSONObject tmpCar=new JSONObject();
					String pam="'"+phone+"',";
					pam=pam+"'"+datalist.getJSONObject(i).getString("carid")+"',";
					pam=pam+"'"+datalist.getJSONObject(i).getString("userid")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("applyid")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("carid")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("cartype")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("engineno")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("enterbjstart")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("enterbjend")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("licenseno")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("status")+"',";
					pam=pam+"'"+carapplyarr.getJSONObject(j).getString("userid")+"'";
					String sql="INSERT INTO jjz_main (phone,jjzx_carid,jjzx_userid,applyid,carid,cartype,engineno,enterbjstart,enterbjend,licenseno,status,userid) values ("+pam+");";
					System.out.println(sql);
					if(!con.checkId("select id from jjz_main where applyid='"+carapplyarr.getJSONObject(j).getString("applyid")+"'")){
						con.insertSql(sql);
					}
					tmpCar.put("licenseno", carapplyarr.getJSONObject(j).getString("licenseno"));
					tmpCar.put("enterbjstart", carapplyarr.getJSONObject(j).getString("enterbjstart"));
					tmpCar.put("enterbjend", carapplyarr.getJSONObject(j).getString("enterbjend"));
					tmpCar.put("status", carapplyarr.getJSONObject(j).getString("status"));
					resCar.add(tmpCar);
				}
			}
		con.closeCon();	
		}else{
		//获取数据异常
			
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resCar;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	@Override
	public void destroy() {
		// ConMysql.closeCon();
	}

}
